package dispatcher.group.gui.floor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import state.Globals;
import system.Constant;

import dispatcher.group.gui.floor.state.FloorStruct;
import dispatcher.gui.state.GroupDispatcherStateHandler;
import dispatcher.misc.MiscGlobals;

/**
 * This class is the graphics representation of the floor.
 * @author Caleb
 *
 */
public class FloorPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6525199852457549934L;

	private static final int DEFAULT_FLOOR_HEIGHT = 100;
	private static final int DEFAULT_FLOOR_WIDTH = 200;
	
	private int floorNumber;
	
	private BufferedImage image;
	private JButton upButton;
	private JButton downButton;
	
	/**
	 * The main constructor
	 * @param floorNumber The floor number of this graphics panel
	 */
	public FloorPanel(int floorNumber){
		this.floorNumber = floorNumber;
		
		// Creates the fixed panel size
		this.setPreferredSize(new Dimension(DEFAULT_FLOOR_WIDTH, DEFAULT_FLOOR_HEIGHT));
		this.setMaximumSize(new Dimension(DEFAULT_FLOOR_WIDTH, DEFAULT_FLOOR_HEIGHT));
		this.setLayout(new BorderLayout());
		
		
		this.image = MiscGlobals.STICK_IMAGE;
		
		createFloorBorder();
		createHallCalls();
		
	}
	/**
	 * This method create the bordering around the panel. It will also provide the floor number in
	 * the border
	 */
	public void createFloorBorder(){
		TitledBorder shaft = null;
		if (floorNumber == 0){
			shaft = BorderFactory.createTitledBorder("Lobby");
		}else{
			shaft = BorderFactory.createTitledBorder("" + (floorNumber + 1 - Constant.DEFAULT_FLOOR_START));
		}
		shaft.setTitleJustification(TitledBorder.CENTER);
		shaft.setTitleColor(Color.BLACK);
		this.setBorder(shaft);
	}
	
	/**
	 * This method will create the up and down buttons found in the panel
	 */
	public void createHallCalls(){
		JPanel holder = new JPanel();
		
		// Creates a border around the buttons
		Border b = BorderFactory.createLineBorder(Color.BLACK);
		holder.setBorder(b);
		holder.setLayout(new BorderLayout());
		
		// Fix the max size of the holder
		holder.setPreferredSize(new Dimension(50, 60));
		holder.setMaximumSize(new Dimension(50, 60));
		
		// Create the up button
		upButton = new JButton("^");
		upButton.setEnabled(false);
		upButton.setBackground(Color.GRAY);
		
		// Create the down button
		downButton = new JButton("v");
		downButton.setEnabled(false);
		downButton.setBackground(Color.GRAY);
		
		holder.add(upButton, BorderLayout.NORTH);
		holder.add(downButton, BorderLayout.SOUTH);
		
		// Adds holder to main panel
		this.add(holder, BorderLayout.EAST);
	}
	
	/**
	 * A custom paint method
	 */
	@Override
	public void paint(Graphics g){
		super.paint(g);
		
		boolean upPressed = false;
		boolean downPressed = false;
		
		// Retrieves the floor struct from the local memory
		FloorStruct struct = GroupDispatcherStateHandler.getInstance().getFloorStatus((byte)floorNumber);
		
		if (struct != null){
			synchronized(struct){
				// Copies the up and down state of the floor
				upPressed = struct.isUpPressed();
				downPressed = struct.isDownPressed();
			}
		}else{
			Globals.getLogger().log("Failed to paint floor panel with struct number, " + floorNumber);
			return;
		}
		// If a button has been pressed, draw a stick figure to denote a person is at the floor
		if(upPressed || downPressed){
			if (image != null){
				g.drawImage(image, 75, 20, 50, 60, null);
			}
		}
		// If a button is pressed, set the appropriate button background to green 
		if(upPressed){
			upButton.setBackground(Color.GREEN);
		}else{
			upButton.setBackground(Color.GRAY);
		}
		
		if (downPressed){
			downButton.setBackground(Color.GREEN);
		}else{
			downButton.setBackground(Color.GRAY);
		}
		
		
	}
}
